﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;

public partial class Agency_Company_SC_Select : System.Web.UI.Page
{
    int projectid,AccountId_TB;

    protected void Page_Load(object sender, EventArgs e)
    {
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAttorney);
        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
            projectid = SysFun.ToInt(SysFun.IsSQL(Request.QueryString["projectid"]));
            AccountId_TB = SysFun.ToInt(SysFun.IsSQL(Request.QueryString["AccountId_TB"]));
            if (Request.Form["selcompany"] == "确定选取")
            {
                SelCompany();
            }
            dgShowData();
        }
    }
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        e.Row.Cells[3].Visible = false;

    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[2].Text = "<input type=checkbox name=\'chkSelect\' value=\"" + e.Row.Cells[3].Text + "\" />";
        }

    }

    protected void dgShowData()
    {
        Db dbshow = new Db();
        DbPage dp = new DbPage(dbshow);
        dp.Table = "company";
        dp.Key = "AccountId";
        dp.Where = fctConditions();
        dp.SelectField = "companyname,accountid,UserCode";
        dp.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);
        dp.PageSize = 20;
        System.Data.IDataReader dr = dp.ExecuteReader();
        GridView1.DataSource = dr;
        GridView1.DataBind();
        dr.Close();
        dr.Dispose();
        dp.Db.Close();
        dbshow.Close();
        this.DbPageFlip1.DbPage = dp;
    }

    public string fctConditions()
    {
        string _strWhere = "    sort=0 and accountid not in (select AccountId_SC from project_bid_company where projectid=" + projectid.ToString() + " and accountid_tb=" + AccountId_TB.ToString() + " )";

        //企业名称
        string _strCompanyName = txtCompanyName.Text.Trim();
        if (_strCompanyName != "")
        {
            _strWhere = _strWhere + " and companyname like '%" + _strCompanyName + "%' ";
        }
        //企业注册号
        string _strUserCode = txtUserCode_TB.Text.Trim();
        if (_strUserCode != "")
        {
            _strWhere = _strWhere + " and UserCode like '%" + _strUserCode + "%'";
        }

        return _strWhere;
    }

    protected void SelCompany()
    {
        string strSql;
        string companyid;

        Db db = new Db();
        //if (CheckAll.Checked)
        //{
        //strSql = "Delete ProjectCompanyAttribution Where ProjectId=" + projectid.ToString() + "";
        //db.Command(strSql);
        //strSql = fctConditions();
        //db.Command(strSql);
        //db.Close();
        //Response.Write("<script>window.alert('保存成功！');</script>");
        //}
        if (Request.Form["chkSelect"] != null)
        {
            companyid = Request.Form["chkSelect"].ToString();
            strSql = "insert into project_bid_company (AccountId_TB, ProjectId, AccountId_SC, CompanyName_SC) select " + AccountId_TB.ToString() +
                "," + projectid.ToString() + ",accountid,companyname from company where accountid in (" + companyid +
                ") and accountid not in (select AccountId_SC from project_bid_company where projectid=" + projectid.ToString() 
                + " and accountid_tb=" + AccountId_TB.ToString() +" )";
            db.Command(strSql);
            db.Close();
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "插入", "ProjectCompanyAttribution", strSql, "../Agency/AddCom.aspx");

            Response.Write("<script>window.alert('保存成功！');</script>");
        }

        Response.Write("<script>window.opener.location.reload();</script>");
        Response.Write("<script>window.close();</script>");
    }
}
